package share.servlet;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.eu.base.io.TextOperator;

import share.storage.DirectoryTree;
import share.storage.io.File;

public class ExchangeServlet extends HttpServlet {

	private static final long serialVersionUID = 1349308135558168233L;

	public ExchangeServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		File root = DirectoryTree.getInstance().getRoot();
		java.io.File xmlFile = new java.io.File(getServletContext().getRealPath("exchange.xml"));
		TextOperator tr=null;
		boolean createXml=false;
		if(!xmlFile.exists()){
			createXml=true;
			xmlFile.createNewFile();
		}
		tr= new TextOperator(xmlFile);
		if (!createXml&&root.lastModified() < xmlFile.lastModified()) {
			response.getWriter().print(tr.readAll());
			tr.close();
			return;
		}
		//update exchange.xml
		String xml=buildXML();
		synchronized(ExchangeServlet.class){
			if (!createXml&&root.lastModified() < xmlFile.lastModified()) {
				response.getWriter().print(tr.readAll());
				tr.close();
				return;
			}
			tr.overWrite(xml);
			response.getWriter().print(xml);
		}
	}

	public void init() throws ServletException {
		// Put your code here
	}
	
	private static String buildXML() {
		DirectoryTree dt = DirectoryTree.getInstance();
		Document doc = DocumentFactory.getInstance().createDocument();
		Element root = doc.addElement("EnterPoint");
		root.addAttribute("startPath", "/");
		walkTree(dt.getRoot(),true, root, doc);
		OutputFormat format = new OutputFormat("\t", true);
		format.setEncoding("UTF-8");
		StringWriter sw = new StringWriter();
		XMLWriter xw = new XMLWriter(sw, format);
		try {
			xw.write(doc);
			xw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sw.toString();
	}

	private static void walkTree(File root, boolean isRoot, Element el,
			Document doc) {
		if (!isRoot) {
			el = el.addElement("File");
			fillElement(root, el);
		}
		if (root.isFile()) {
			return;
		}
		File[] fs = root.getChildren();
		if (fs == null)
			return;
		for (int i = 0; i < fs.length; i++) {
			walkTree(fs[i], false, el, doc);
		}
	}

	public static void fillElement(File file, Element element) {
		element.addAttribute("name", file.getFilename());
		element.addAttribute("size", String.valueOf(file.getSize()));
		element.addAttribute("lastModified", String.valueOf(file.lastModified()));
		element.addAttribute("type", file.getFileType().toString());
		element.addAttribute("isDir", String.valueOf(file.isDirectory()));
		String describe=file.getDescribe();
		if(describe!=null){
			element.addElement("describe").addText(describe);
		}
	}

}
